home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0151_Extended TIniFile Component.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  8.3 KB  |  251 lines

  1. {****************************************************************
  2.  *     TExtIniF class                                                                                            *
  3.  *    created and copyright by Ferdinand Soethe 1996                          *
  4.  *    (email: f.soethe@oln.comlink.apc.org)                                                *
  5.  *                                                                                                                            *
  6.  *    You may use this source code in your applications           *
  7.  *    (a word of credit would be nice) but you must not                     *
  8.  *    resell it as part of a library or publish it in paper-            *
  9.  *    or electronic form without asking my permission.                        *
  10.  *                                                                                                                            *
  11.  *    TExtIniF extends Delphi's TIniFile to simplify saving a         *
  12.  *    components state to an INI-File. After creation you can         *
  13.  *    register any number of components and    save and retrieve     *
  14.  *  their settings with just one call to StoreObjectStates.            *
  15.  *                                                                                                                            *
  16.  ****************************************************************}
  17.  
  18. unit extINIF;
  19.  
  20. interface
  21.  
  22. uses
  23.     {unfortunately we need to include a units of classes
  24.       that we want to be able to store}
  25.   IniFiles, Files, Classes, Forms, StdCtrls, FileCtrl, Menus,
  26.   sysUtils, TabNotBK;
  27.  
  28. type
  29.     EExtIniFError = class(Exception);
  30.     TExtIniF = class(TIniFile)
  31.   private
  32.     { Private-Deklarationen }
  33.         FAutoStore: boolean;    {store all objects states before TExtIniF is destroyed}
  34.         FRegObjects: TStringList; {list of all registered objects}
  35.         FIniSection: String;    {Name of [section] where values are stored}
  36.   protected
  37.     { Protected-Deklarationen }
  38.   public
  39.     { Public-Deklarationen }
  40.         constructor create(IniFName: TFileName);
  41.         destructor destroy; override;
  42.         {find the ini section for a registered object}
  43.         function GetIniSection(obj: TObject): string;
  44.         {Add a component to the list of objects}
  45.         procedure RegisterObject(obj: TObject; INISection: string);
  46.         {Remove a component to the list of objects}
  47.     procedure UnRegisterObject(obj: TObject; INISection: string);
  48.         {Retrieve the setting of a single Object}
  49.     procedure ReStoreObjectState(obj: TObject; INISection: string);
  50.         {Restore states of all registered objects}
  51.         procedure RestoreObjectStates;
  52.         {Restore states of all registered objects}
  53.         procedure StoreObjectState(obj: TObject; INISection: string);
  54.         {Store state of a single object}
  55.         procedure StoreObjectStates;
  56.         {Store states of all registered objects}
  57.   published
  58.     { Published-Deklarationen }
  59.         property AutoStore: boolean read FAutoStore write FAutoStore;
  60.         property IniSection: string read FIniSection write FIniSection;
  61.   end;
  62.  
  63. implementation
  64.  
  65. {  find the section string to a registered object
  66.    if not registered or section string is empty
  67.    return default value}
  68. function TExtIniF.GetIniSection(obj: TObject): string;
  69. var
  70.     index: integer;
  71. begin
  72.     index:= FRegObjects.indexOfObject(obj);
  73.     if ( index > -1 ) then begin
  74.         result:= FRegObjects.strings[index];
  75.         if result = '' then
  76.             result:= FIniSection;
  77.     end else
  78.         result:= FIniSection;
  79. end; {GetIniSection}
  80.  
  81. { }
  82. constructor TExtIniF.create(IniFName: TFileName);
  83. begin
  84.     {if you don't pass your own name for the ini-File, it will be the name
  85.      of your exe-file with the extension '*.INI'}
  86.     if ( IniFName = '' ) then IniFName:= ExtraxtFileBaseName(application.exename)+'.ini';
  87.     inherited create(IniFName);
  88.   FRegObjects:= TStringList.Create;
  89.     FIniSection:= 'Options';
  90. end;
  91.  
  92. { }
  93. destructor TExtIniF.Destroy;
  94. begin
  95.     {If AutoStore is set, values are stored
  96.      before TExtIniF-Object is destroyed}
  97.     if FAutoStore then StoreObjectStates;
  98.     FRegObjects.destroy;
  99.     inherited destroy;
  100. end;
  101.  
  102. {  Add an object to the list of monitored objects. If you pass an empty string
  103.    for INISection, the default value will apply and no name will be stored}
  104. procedure TExtIniF.RegisterObject(obj: TObject; INISection: string);
  105. begin
  106.     {check if object is already registered}
  107.     if ( FRegObjects.indexOfObject(obj) = -1 ) then
  108.         FRegObjects.addObject(INISection,obj);
  109. end;
  110.  
  111. {  Remove an object from the list of monitored objects.}
  112. procedure TExtIniF.UnRegisterObject(obj: TObject; INISection: string);
  113. var
  114.     index: integer;
  115. begin
  116.     index:= FRegObjects.indexOfObject(obj);
  117.     if ( index > -1 ) then
  118.         FRegObjects.delete(index);
  119. end;
  120.  
  121.  
  122. {  Restores the name of an object from the INI-File
  123.    Note: When there is no entry in the INI-File, the object's value
  124.                   is not changed.}
  125. procedure TExtIniF.ReStoreObjectState(obj: TObject; INISection: string);
  126. var
  127.     strBuf: string;
  128. begin
  129.     if ( INISection = '' ) then INISection:= FIniSection;
  130.     {the next lines check for the type of object and
  131.      restore whatever property we would like to store of that object
  132.      if you make changes here you will need to make changes in
  133.      StoreObjectState as well!!!}
  134.   if (obj.classInfo <> nil ) then begin
  135.     if (obj is TCheckBox) then begin
  136.       with (obj as TCheckBox) do begin
  137.                 {Checkboxes: restore checked state}
  138.         checked:= readBool(INISection,Name,checked);
  139.       end;
  140.     end else
  141.     if (obj is TEdit) then begin
  142.       with (obj as TEdit) do begin
  143.                 {Editfield: restore text}
  144.         text:= readString(INISection,Name,text);
  145.       end;
  146.     end else
  147.     if (obj is TMenuItem) then begin
  148.       with (obj as TMenuItem) do begin
  149.                 {Menuitem: restore checked state}
  150.         checked:= readBool(INISection,Name,checked);
  151.       end;
  152.     end else
  153.         if (obj is TTabbedNoteBook) then begin
  154.         with (obj as TTabbedNoteBook) do begin
  155.                     {Notebook: restore open Tab}
  156.           pageIndex:= readInteger(INISection,Name,pageIndex);
  157.         end;
  158.     end else
  159.     if (obj is TDriveComboBox) then begin
  160.       with (obj as TDriveComboBox) do begin
  161.                 {DriveCombo: restore selected drive}
  162.                 strBuf:= readString(INISection,Name,Drive);
  163.         Drive:= strBuf[1];
  164.       end;
  165.     end else
  166.     if (obj is TDirectoryListBox) then begin
  167.       with (obj as TDirectoryListBox) do begin
  168.                 {DirectoryList: restore current directory}
  169.         Directory:= readString(INISection,Name,Directory);
  170.       end;
  171.     end else
  172.         raise EExtIniFError.create('This object is not supported!');
  173.     end;
  174. end;
  175.  
  176. {  Restores the state of all registered objects
  177.    from the INI-File}
  178. procedure TExtIniF.RestoreObjectStates;
  179. var
  180.     objNo: integer;
  181. begin
  182.     {iterate through all registered objects}
  183.     for objNo:= 0 to FRegObjects.count - 1 do
  184.     ReStoreObjectState(FRegObjects.objects[objNo],FRegObjects.strings[objNo]);
  185. end;
  186.  
  187. {  Stores the state of an object to the INI-File}
  188. procedure TExtIniF.StoreObjectState(obj: TObject; INISection: string);
  189. var
  190.         strBuf: string;
  191. begin
  192.     if ( INISection = '' ) then INISection:= FIniSection;
  193.  
  194.     {the next lines check for the type of object and
  195.      store whatever property we would like to store of that object
  196.      if you make changes here you will need to make changes in
  197.      ReStoreObjectState as well!!!}
  198.  
  199.   if (obj.classInfo <> nil ) then begin
  200.       if (obj is TCheckBox) then begin
  201.                 {Checkboxes: store checked state}
  202.         with (obj as TCheckBox) do begin
  203.           writeBool(INISection,Name,checked);
  204.         end;
  205.       end else
  206.       if (obj is TEdit) then begin
  207.                 {Editfield: store text}
  208.         with (obj as TEdit) do begin
  209.           writeString(INISection,Name,text);
  210.         end;
  211.       end else
  212.       if (obj is TMenuItem) then begin
  213.                 {Menuitem: restore checked state}
  214.         with (obj as TMenuItem) do begin
  215.           writeBool(INISection,Name,checked);
  216.         end;
  217.       end else
  218.             if (obj is TTabbedNoteBook) then begin
  219.         with (obj as TTabbedNoteBook) do begin
  220.                     {Notebook: restore open Tab}
  221.           writeInteger(INISection,Name,pageIndex);
  222.         end;
  223.         end else
  224.             if (obj is TDriveComboBox) then begin
  225.           with (obj as TDriveComboBox) do begin
  226.                     {DriveCombo: restore selected drive}
  227.                     writeString(INISection,Name,Drive);
  228.           end;
  229.         end else
  230.             if (obj is TDirectoryListBox) then begin
  231.                 {DirectoryList: restore current directory}
  232.         with (obj as TDirectoryListBox) do begin
  233.           writeString(INISection,Name,Directory);
  234.         end;
  235.         end else
  236.             raise EExtIniFError.create('This object is not supported!');
  237.         end;
  238. end;
  239.  
  240. {  Stores the state of all registered objects
  241.    to the INI-File}
  242. procedure TExtIniF.StoreObjectStates;
  243. var
  244.     objNo: integer;
  245. begin
  246.     for objNo:= 0 to FRegObjects.count - 1 do
  247.     StoreObjectState(FRegObjects.objects[objNo],FRegObjects.strings[objNo]);
  248. end;
  249.  
  250.  
  251. end.